package com.souku.finance.application.utils;

import android.app.Activity;
import android.os.Build;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.souku.finance.application.R;

/**
 * Created by Android on 2016/12/23.
 */

public class ViewUtil {

    public static void setDelete(final EditText editText, final ImageView delete, final ImageView show) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (delete != null) {
                    if (s.length() == 0) {
                        delete.setVisibility(View.GONE);
                    } else {
                        delete.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (delete != null) {
                    if (hasFocus && !MyTextUtil.isEmpty(editText.getText().toString())) {
                        delete.setVisibility(View.VISIBLE);
                    } else {
                        delete.setVisibility(View.GONE);
                    }
                }
            }
        });
        if (delete != null) {
            delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editText.setText("");
                }
            });
        }
        if (show != null) {
            show.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (editText.getInputType() == 129) {
                        editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        show.setImageResource(R.drawable.pass_show);
                    } else {
                        editText.setInputType(129);
                        show.setImageResource(R.drawable.pass_hidden);
                    }
                    editText.setSelection(editText.getText().toString().trim().length());
                }
            });
        }
    }


    public static void setCountDown(final Button button, final View.OnClickListener listener) {
        final CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                button.setText(millisUntilFinished / 1000 + "S");
                button.setEnabled(false);
            }

            @Override
            public void onFinish() {
                button.setText("立即获取");
                button.setEnabled(true);
            }
        };
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                button.setEnabled(false);
                timer.start();
            }
        });
    }

    public static View getContentView(Activity ac) {
        ViewGroup view = (ViewGroup) ac.getWindow().getDecorView();
        FrameLayout content = (FrameLayout) view.findViewById(android.R.id.content);
        return content.getChildAt(0);
    }

    public static void setTextViewSize(double db, TextView textView, boolean isBig) {
        int length = MyTextUtil.doubleFormat1(db).length();
        if (isBig) {
            if (length < 6) {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 45);
            } else if (length < 9) {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 38);
            } else if (length < 12) {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 32);
            } else {
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 26);
            }
        } else {
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 32);
        }
    }

    public static void setColor(Activity activity, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 设置状态栏透明
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 生成一个状态栏大小的矩形
            View statusView = createStatusView(activity, color);
            // 添加 statusView 到布局中
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            decorView.addView(statusView);
            // 设置根布局的参数
            ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(true);
            rootView.setClipToPadding(true);
        }
    }

    private static View createStatusView(Activity activity, int color) {
        // 获得状态栏高度
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        int statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);

        // 绘制一个和状态栏一样高的矩形
        View statusView = new View(activity);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                statusBarHeight);
        statusView.setLayoutParams(params);
        statusView.setBackgroundColor(color);
        return statusView;
    }
}
